/*
https://leetcode-cn.com/problems/queue-reconstruction-by-height/solution/gen-ju-shen-gao-zhong-jian-dui-lie-by-leetcode-sol/
 */
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;

public class Solution406 {
    public int[][] reconstructQueue(int[][] people) {
        Arrays.sort(people,(a,b)->a[0]==b[0]?a[1]-b[1]:b[0]-a[0]);
        List<int[]> list=new LinkedList<>();
        for (int[] person : people) {
            list.add(person[1],new int[]{person[0],person[1]});
        }
        int[][] ans=new int[people.length][2];
        for (int i=0;i<list.size();i++){
            ans[i][0]=list.get(i)[0];
            ans[i][1]=list.get(i)[1];
        }
        return ans;
    }

    public static void main(String[] args) {
        int[][] ans=new Solution406().reconstructQueue(new int[][]{{7,0},{4,4},{7,1},{5,0},{6,1},{5,2}});
        for (int[] an : ans) {
            System.out.println(Arrays.toString(an));
        }
    }
}
